/**
 * 
 */
package com.sys.console.business.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.sys.console.business.SysModuleService;
import com.sys.console.cookie.Constant_Cookie;
import com.sys.console.mapper.Sys_ModuleMapper;
import com.sys.console.mapper.Sys_Module_AuthorityMapper;
import com.sys.console.mapper.Sys_Role_AuthorityMapper;
import com.sys.console.mapper.Sys_User_RoleMapper;
import com.sys.console.pojo.MeunAuthorityTreeJsonDto;
import com.sys.console.pojo.SysModuleDto;
import com.sys.console.pojo.SysModuleMeunDto;
import com.sys.console.pojo.SysRoleAuthorityKeyDto;
import com.sys.console.pojo.SysRoleModuleDto;
import com.sys.console.pojo.SysUserRoleKeyDto;
import com.sys.console.pojo.Sys_Module;
import com.sys.console.pojo.Sys_Module_Authority_Key;
import com.sys.console.pojo.Sys_User;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;



/**
 * 2014-11-05
 * 
 * @author 陈成军 模块类别管理
 */
@Component
public class SysModuleServiceImpl implements SysModuleService {

	@Autowired	Sys_ModuleMapper sysModuleMapper;
	@Autowired	Sys_User_RoleMapper sysUserRoleMapper;
	@Autowired	Sys_Role_AuthorityMapper sysRoleAuthorityMapper;
	@Autowired Sys_Module_AuthorityMapper sysModuleAuthorityMapper;

	/**
	 * 删除
	 */
	@Override
	public int deleteByPrimaryKey(Integer moduletypeid) throws Exception {
		// TODO Auto-generated method stub
		if (moduletypeid == null)
			throw new ObjException(ObjResultCode.DATA_ERROR);
		return sysModuleMapper.deleteByPrimaryKey(moduletypeid);
	}

	/**
	 * 新增
	 */
	@Override
	public int insert(Sys_Module record) throws Exception {
		// TODO Auto-generated method stub
	
		if (record == null)
			throw new ObjException(ObjResultCode.DATA_ERROR);
		
		return sysModuleMapper.insert(record);
	}

	/**
	 * 详情
	 */
	@Override
	public Sys_Module selectByPrimaryKey(Integer Moduleid) throws Exception {
		// TODO Auto-generated method stub
		if (Moduleid != null&&Moduleid>0)
			return sysModuleMapper.selectByPrimaryKey(Moduleid);
		return null;
	}


	/**
	 * 修改
	 */
	@Override
	public int updateByPrimaryKey(Sys_Module record) throws Exception {
		// TODO Auto-generated method stub
		if (record == null)
			throw new ObjException(ObjResultCode.DATA_ERROR);
		return sysModuleMapper.updateByPrimaryKey(record);
	}
		
	/**
	 * 优化登录用户菜单
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public ArrayList<SysModuleMeunDto> selectUserModuleByList(Sys_Module record) throws Exception {	
		Sys_User sysUser = (Sys_User) SecurityUtils.getSubject().getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
		Integer  userid = sysUser.getUid();
		if(sysUser!=null){
			ArrayList<SysModuleMeunDto> listmenu = (ArrayList<SysModuleMeunDto>) SecurityUtils.getSubject().getSession().getAttribute(Constant_Cookie.SYS_MODULE_MEUN_SESSION);
			if(listmenu != null && listmenu.size()>0){
				return listmenu;
			}
		}
		ArrayList<SysModuleMeunDto> plist = new ArrayList<SysModuleMeunDto>();
		List<SysUserRoleKeyDto> roleAuthList = sysUserRoleMapper.selectUserRoleKeyListByUid(userid);
		//是否为超级管理员
		Integer isAdmin = sysUser.getIsadmin();
		//取出所有节点
		record.setPid(0);
		record.setEnable(1);
		List<SysModuleMeunDto> meunList = sysModuleMapper.selectMeumListByRoot(record);
		for(SysModuleMeunDto p_menu:meunList){
			//取出所有子节点
			int i = 0;//用于判断是否有子菜单
			ArrayList<SysModuleMeunDto> childlist = new ArrayList<SysModuleMeunDto>();
			for(SysModuleMeunDto s_menu:p_menu.getChildList()){
				//为超级管理员时拥有所有菜单
				if(isAdmin==1){
					childlist.add(s_menu);
					i++;
				}else{
					int s_mid = s_menu.getMid(); //模块ID
					for(SysUserRoleKeyDto authList:roleAuthList){
						for(SysRoleAuthorityKeyDto entity:authList.getRoleAuthorityKeyDtoList()){
							int mid = entity.getMid();//拥有模块的Id
							if(mid == s_mid){
								childlist.add(s_menu);
								i++;
								break;
							}
						}
					}
				}
			}
			//如果有节点，输出父节点
			if(i>0){
				p_menu.setChildList(childlist);
				plist.add(p_menu);
			}
		}
		SecurityUtils.getSubject().getSession().setAttribute(Constant_Cookie.SYS_MODULE_MEUN_SESSION,plist);
		return plist;
	}

	@Override
	public ArrayList<Sys_Module> selectByPid(Sys_Module record) throws Exception {
		// TODO Auto-generated method stub
		return sysModuleMapper.selectByPid(record);
	}
	
	@Override
	public List<SysRoleModuleDto> selectRoleMeunAuthListByPid(Sys_Module record) throws Exception {
		// TODO Auto-generated method stub
		return sysModuleMapper.selectRoleMeunAuthListByPid(record);
	}

	@Override
	public List<SysModuleDto> selectByTree(Sys_Module record) throws Exception {
		// TODO Auto-generated method stub
		return sysModuleMapper.selectTreeByDto(record);
	}

	@Override
	public int saveOrUpdate(Sys_Module module) throws Exception {
		int success = 0;
		if(module!=null&&module.getMid()!=null&&module.getMid()>0){
			success = this.updateByPrimaryKey(module); //修改模块信息
			//删除原有数据
			if(success>0)
				sysModuleAuthorityMapper.deleteByMid(module.getMid());
		}else{
			success = this.insert(module);
		}
		if (success > 0) {
			if(StringUtils.hasText(module.getAuthority())){
				String[] authItem = module.getAuthority().split("\\#",-1);
				for (int i = 0; i < authItem.length; i++) {
					if(StringUtils.hasText(authItem[i])){
						int aid =Integer.parseInt(authItem[i]);
						Sys_Module_Authority_Key record = new Sys_Module_Authority_Key();
						record.setMid(module.getMid());
						record.setAid(aid);
						sysModuleAuthorityMapper.insert(record);
					}
				}
			}
		}
		return success;
	}

	@Override
	public List<MeunAuthorityTreeJsonDto> selectMeunAuthTree(Sys_Module entity) throws Exception {
		List<MeunAuthorityTreeJsonDto> list = sysModuleMapper.selectMeunAuthTree(entity);
		List<MeunAuthorityTreeJsonDto> dtolist = new ArrayList<MeunAuthorityTreeJsonDto>();
		for(MeunAuthorityTreeJsonDto dto:list){
			dtolist.add(dto);
			if(dto.getChildrenAuth()!=null && dto.getChildrenAuth().size()>0){
				for(MeunAuthorityTreeJsonDto cdto:dto.getChildrenAuth()){
					dtolist.add(cdto);
				}
				dto.setChildrenAuth(null);
			}
		}
		return dtolist;
	}

	
}
